jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.text.environmentHeader.EnvironmentHeader", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        	plugin.environmentCacheId = "SPM_DEPLOYMENT_ENVIRONMENT_CACHE";
        	
        	plugin.prototype.subscribedToPolling;
        	plugin.prototype.screenId;
        
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.setUpComponent = function(screenId) {
            	this.subscribedToPolling = false;
            	this.screenId = screenId;
	            tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.environmentChangeSubject, this.environmentSelected, this);
	            tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.general.NavigationHandler.mainPageDisplayTopic, this.handleDisplayScreen, this);
            }
            
            plugin.prototype.environmentSelected = function(subject, message, headerObj) {
            	var envId = message.environmentId;
            	headerObj.callGetEnvironments(envId);
            	
            	if(!headerObj.subscribedToPolling)
            	{
            		headerObj.subscribedToPolling = true; 
            		tibco.admin.monitormanage.general.PollingHandler.register(tibco.admin.monitormanage.general.PollingHandler.GENERAL_POLLING_HANDLER_ID, headerObj, headerObj.updateComponent, tibco.admin.monitormanage.uicomponents.page.deployment.Deployment.environmentScreenId); 
            		tibco.admin.monitormanage.general.RefreshHandler.register(headerObj, headerObj.updateComponent, tibco.admin.monitormanage.uicomponents.page.deployment.Deployment.environmentScreenId); 
            	}
            }
            
            plugin.prototype.handleDisplayScreen = function(subject, message, headerObj) {
            	if(message.screenId == headerObj.screenId)
            	{
            		var envId = message.environmentId;
            		if(!envId) 
            		{ envId = tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.getCurrentEnvironmentId(); }
            		if(envId && envId != "" && envId != tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.allEnvironmentsId)
            		{ 
	            		var msg = { environmentId:envId };
            			var subject = tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.environmentChangeSubject;
            			headerObj.environmentSelected(subject, msg, headerObj);
            		}
            	}
            }
            
            plugin.prototype.updateComponent = function() {
				this.callGetEnvironments();
			}
            
            //call this method to begin the service call (eg.service.callgetEnvironments();)
    		plugin.prototype.callGetEnvironments = function(environmentId) {
    		
    			if (tibco.admin.Main.getServer().getEnv("MODE")) {
                    // mode is live
                    var objService = tibco.admin.Main.loadService(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/text/environmentHeader/rules/environmentRules.xml", "getEnvironments");
                    objService.setEndpointURL(plugin.wsEndpointURL);
		    		// this setInboundURL should not be required...only here because of a current AMX framework bug
		    		objService.setInboundURL("tibco/admin/monitormanage/uicomponents/text/environmentHeader/data/environmentData.xml");
                }
                else {
                    var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/text/environmentHeader/rules/environmentRules.xml"), "getEnvironments", null, null);
                    // mode is static
                    objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/text/environmentHeader/data/environmentData.xml"));
                }
                
                objService.envHeaderObj = this;
                
                if(!environmentId)
				{ environmentId = tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.getCurrentEnvironmentId(); }
				objService.environmentId = environmentId;
                
                // initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("numServiceAssemblies");
				metricsList.add("numServiceAssembliesRunning");
				metricsList.add("numServices");
				metricsList.add("numServicesRunning");
				objService.metricsList = metricsList;

      			//subscribe and call
      			objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetEnvironmentsSuccess);
      			objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetEnvironmentsError);
      			objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetEnvironmentsInvalid);
      			objService.doCall();
    		};

    		plugin.prototype.ongetEnvironmentsSuccess = function(objEvent) {
      			//var responseXML = objEvent.target.getInboundDocument();
      			//objEvent.target.getServer().alert("Success","The service call was successful.");
      			var server = tibco.admin.Main.getServer();
      			var envHeaderObj = objEvent.target.envHeaderObj;
      			var envLabel = envHeaderObj.getDescendantOfName("environmentLabel");
      			var envDoc = server.Cache.getDocument(plugin.environmentCacheId);
      			var record = envDoc.getFirstChild();
				envLabel.setText(record.getAttribute("name"));
				envLabel.repaint();

				var nodesRunningLabel = envHeaderObj.getDescendantOfName("serviceAssembliesRunningLabel");
				var nodesRunningText = record.getAttribute("numServiceAssembliesRunning") + server.getDynamicProperty("@MM@general@ofLabel") + record.getAttribute("numServiceAssemblies") + server.getDynamicProperty("@MM@general@svcAsblRunningText");
				nodesRunningLabel.setText(nodesRunningText);
				nodesRunningLabel.repaint();
				
				var servicesRunningLabel = envHeaderObj.getDescendantOfName("servicesRunningLabel");
				var servicesRunningText = record.getAttribute("numServicesRunning") + server.getDynamicProperty("@MM@general@ofLabel") + record.getAttribute("numServices") + server.getDynamicProperty("@MM@general@servicesRunningText");
				servicesRunningLabel.setText(servicesRunningText);
				servicesRunningLabel.repaint();
    		};
    		
    		plugin.prototype.ongetEnvironmentsError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			objEvent.target.getServer().alert("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		}

    		plugin.prototype.ongetEnvironmentsInvalid = function(objEvent) {
      			objEvent.target.getServer().alert("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    		}

        });